<template>
  <div class="p-4">
    <BasicTable @register="registerTable">
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '编辑',
              onClick: handleEdit.bind(null, record),
              auth: 'other', // 根据权限控制是否显示: 无权限，不显示
            },
            {
              label: '删除',
              icon: 'ic:outline-delete-outline',
              onClick: handleDelete.bind(null, record),
              auth: 'super', // 根据权限控制是否显示: 有权限，会显示
            },
          ]"
          :dropDownActions="[
            {
              label: '启用',
              popConfirm: {
                title: '是否启用？',
                confirm: handleOpen.bind(null, record),
              },
              ifShow: (_action) => {
                return record.status !== 'enable'; // 根据业务控制是否显示: 非enable状态的不显示启用按钮
              },
            },
            {
              label: '禁用',
              popConfirm: {
                title: '是否禁用？',
                confirm: handleOpen.bind(null, record),
              },
              ifShow: () => {
                return record.status === 'enable'; // 根据业务控制是否显示: enable状态的显示禁用按钮
              },
            },
            {
              label: '同时控制',
              popConfirm: {
                title: '是否动态显示？',
                confirm: handleOpen.bind(null, record),
              },
              auth: 'super', // 同时根据权限和业务控制是否显示
              ifShow: () => {
                return true; // 根据业务控制是否显示
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <Modal @register="registerModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import { BasicTable, useTable, BasicColumn, TableAction, FormSchema } from '/@/components/Table';
import { useModal } from '/@/components/Modal';
import { ListApi } from '/@/api/advertising';
  import Modal from './modal.vue';
const columns: BasicColumn[] = [
  {
    title: '姓名',
    dataIndex: 'name',
    // auth: 'test', // 根据权限控制是否显示: 无权限，不显示
  },
  {
    title: '地址',
    dataIndex: 'address',
    // auth: 'super', // 同时根据权限控制是否显示
    // ifShow: (_column) => {
    //   return true; // 根据业务控制是否显示
    // },
  },
];
const colProps = {
  span: 6,
};
const schemas: FormSchema[] = [
  {
    label: '标题：',
    field: 'title',
    component: 'Input',
    colProps,
  },
  // {
  //   label: '类别：',
  //   field: 'type',
  //   component: 'Select',
  //   componentProps: {
  //     options: ADTypes,
  //   },
  //   colProps,
  // },
  {
    label: '状态：',
    field: 'status',
    component: 'Select',
    // componentProps: {
    //   options: onlineStatusType,
    // },
    colProps,
  },
];
export default defineComponent({
  components: { BasicTable, TableAction,  Modal, },
  setup() {
    const [registerModal, { openModal }] = useModal();
    const [registerTable] = useTable({
      // title: 'TableAction组件及固定列示例',
      api: ListApi,
      columns: columns,
      useSearchForm: true,
      // bordered: true,
      formConfig: {
        schemas,
        labelWidth: 100,
      },
      actionColumn: {
        width: 250,
        title: 'Action',
        dataIndex: 'action',
        slots: { customRender: 'action' },
      },
    });
    function handleSuccess() {
      console.log('点击了编辑');
    }
    function handleEdit(record: Recordable) {
      console.log('点击了编辑', record);
      openModal(true, {
        isUpdate: false,
      });
    }
    function handleDelete(record: Recordable) {
      console.log('点击了删除', record);
    }
    function handleOpen(record: Recordable) {
      console.log('点击了启用', record);
    }
    return {
      registerTable,
      handleEdit,
      handleDelete,
      handleOpen,
      registerModal,
      handleSuccess,
    };
  },
});
</script>